package com.gethb.prod.vo;

import lombok.Data;

@Data
public class Result<T> {

    public static final int SUCCESS = 2000;
    public static final int SERVER_EXCEPTION = 500;
    public static final int BAD_PARAM = 400;
    public static final int TOKEN_INVALID = 666;

    private int code;
    private String msg;
    private T body;

    public static <T> Result<T> exception(String msg) {
        return fail(msg, SERVER_EXCEPTION);
    }

    public static <T> Result<T> badParam(String msg) {
        return fail(msg, BAD_PARAM);
    }

    public static <T> Result<T> tokenInvalid() {
        return fail("Token invalid", TOKEN_INVALID);
    }

    public static <T> Result<T> success(T data) {
        Result<T> response = new Result<>();
        response.setCode(SUCCESS);
        response.setBody(data);
        return response;
    }

    private static <T> Result<T> fail(String msg, int errorCode) {
        Result<T> response = new Result<>();
        response.setCode(errorCode);
        response.setMsg(msg);
        return response;
    }
}

